﻿using GalaSoft.MvvmLight;
using System.Collections.ObjectModel;
using System.Linq;
using System;
using MessageWorkflow;
using MessageWorkflow.Message;


namespace SimpleWpfExample.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        #region PropertyNames 

        public const string CurrentUserPropertyName = "CurrentUser";

        #endregion // PropertyNames

        #region Private Fields

        private UserViewModel _currentUser;

        #endregion // Private Fields

        #region Constructor

        public MainViewModel()
        {
            SetupWorkflow();

            LoadTestData();
        }

        private void SetupWorkflow()
        {
            WorkflowChannel.Default.Subscribe<Message.NewAuthenticatedUser>(authenticatedUser =>
            {
                CurrentUser = authenticatedUser.Data;
            });

            WorkflowChannel.Default.Subscribe<NotificationMessage>(message =>
            {
                System.Windows.MessageBox.Show(message.Data);
            });

            WorkflowChannel.Default.Subscribe(new WorkflowHandlerConfiguration(
                WorkflowHandlerFactory.FromLambda<Message.RequestAuthenticatedUser>(request =>
            {
                if (!request.HasResult)
                {
                    if (CurrentUser != null)
                    {
                        request.SetResult(CurrentUser);
                    }
                }
            }))
            {
                Successors = new WorkflowHandlerConfiguration[] 
                { 
                }
            });

        }

        private void LoadTestData()
        {
            Random random = new Random(1);

            Books = new ObservableCollection<BookViewModel>();

            for (int i = 0; i < 50; i++)
            {
                Books.Add(new BookViewModel()
                {
                    Title = Ipsum.GetWords(random.Next(1, 5)),
                    Description = Ipsum.GetWords(random.Next(10, 50)),
                    Price = random.Next(1, 50)
                });
            }

            Users = new ObservableCollection<UserViewModel>(new[]
            {
                new UserViewModel()
                {
                    Name = "John",
                    Password = "test",
                    Books = new ObservableCollection<BookViewModel>(Books.Where(x => random.NextDouble() > 0.8))
                },
                new UserViewModel()
                {
                    Name = "Mary",
                    Password = "test",
                    Books = new ObservableCollection<BookViewModel>(Books.Where(x => random.NextDouble() > 0.7))
                }            
            });
        }

        #endregion // Constructor

        #region Methods 

        private void AuthenticateCurrentUser(Message.RequestAuthenticatedUser request,
            Action callback,
            Action<Exception> onError)
        {
            UserViewModel authenticateUserViewModel = new UserViewModel();


        }

        #endregion // Methods

        #region Public Properties

        public UserViewModel CurrentUser
        {
            get
            {
                return _currentUser;
            }

            set
            {
                if (_currentUser != value)
                {
                    _currentUser = value;

                    RaisePropertyChanged(CurrentUserPropertyName);
                }
            }
        }

        public ObservableCollection<BookViewModel> Books { get; protected set; }

        public ObservableCollection<UserViewModel> Users { get; protected set; }

        #endregion // Public Properties

    }
}